package com.chixing.controller;


import com.chixing.entity.Dish;
import com.chixing.service.IDishService;
import com.chixing.util.Result;
import freemarker.core.ReturnInstruction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */
@RestController  // @ResponseBody (类中的方法的数据是 json格式) + @Controller
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private IDishService dishService;

    @GetMapping("{dishId}")
    public Result getById(@PathVariable("dishId") Integer dishId){
        return  dishService.getById(dishId);
    }
    // 查询所有
    @GetMapping
    public Result getAll(){
        Result result = dishService.getAll();
        return result;
    }
    @PostMapping
    public  Result save(@RequestBody Dish dish){
        dish.setCreateTime(LocalDateTime.now());
        dish.setDishStatus(0);
        dish.setDishImg("images/kfc_food3.png");
        return dishService.save(dish);
    }
    @PutMapping
    public Result update(Dish dish){
        Result result = dishService.update(dish);
        return result;
    }
    @DeleteMapping("{dishId}")
    public  Result delete(@PathVariable("dishId") Integer dishId){
        Result result = dishService.delete(dishId);
        return result;
    }
    @GetMapping("type/{typeId}")
    public Result getByTypeId(@PathVariable("typeId") Integer typeId) {
        return dishService.getByTypeId(typeId);
    }
    @GetMapping("/search")
    public Result searchByName(@RequestParam("name") Integer name) {
        return dishService.getByName(name);
    }

    @GetMapping("/type/{typeId}/search")
    public Result searchByTypeAndName(
            @PathVariable("typeId") Integer typeId,
            @RequestParam("name") Integer name
    ) {
        return dishService.getByType_Name(typeId,name);
    }
}
